package com.flex.commands
{
	
	import com.adobe.cairngorm.business.Responder;
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.view.ViewLocator;
	import com.flex.business.ModelServiceDelegate;
	import com.flex.control.event.ModelCompareEvent;
	import com.flex.view.ModelMainPanelHelper;
	
	import mx.collections.ArrayCollection;
	import mx.core.IMXMLObject;
	import mx.rpc.events.FaultEvent;
	
	
	public class ModelCommand implements Command, Responder
	{
		
		private var methodName:String;
		private var callMethod:Function;
		public function ModelCommand()
		{
			
		}
		
		public function execute(event:CairngormEvent):void
		{
			var delegate:ModelServiceDelegate = new ModelServiceDelegate(this);
			var evt:ModelCompareEvent = event as ModelCompareEvent;
			methodName = evt.methodName;
			switch(methodName){
				case METHOD_GET_MODEL_LIST:
					callMethod = getModelListCall;	
					delegate.getModelList();
					break;
				
				case METHOD_UPDATE_MODEL:
					callMethod = updateModel;	
					delegate.updateModel(evt.param);
					break;
				
				default:
					
					break;			
			}
			
			
		}
		
		public function onResult(event:* = null):void
		{
			callMethod(event.result);
		}
		
				
		public function onFault(event:* = null):void
		{
			var faultEvent:FaultEvent = FaultEvent(event);
			trace("Failed to add template.\n " + faultEvent.fault.faultString);
		}
		
		public function getModelListCall(object:Object):void{
			var modelMainPanelHelper:ModelMainPanelHelper = ViewLocator.getInstance().getViewHelper("modelMainPanelHelper") as ModelMainPanelHelper; 
			modelMainPanelHelper.setModelList(object as ArrayCollection);							
		}	
		
		public function updateModel(obj:Object):void{
			trace("OK");				
		}	
		
		public static const METHOD_GET_MODEL_LIST:String = "getModelList";
		public static const METHOD_UPDATE_MODEL:String = "updateModel";
	}
}